// miniprogram/pages/store/medicdetail/medicdetail.js
const db = wx.cloud.database()
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    comments: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //console.log(options);
    let id = options.id;
    db.collection('MedicineData').doc(id).get().then((res) => {
      //console.log(res)
      this.setData({
        detail: res.data
      });
      db.collection('CommentsData').where({
        CommentID: this.data.detail.MedicID
      }).get().then((res) => {
        //console.log(res)
        if(res.data.length){
          this.setData({
            comments: res.data[0].Comments
          })
        }
        //console.log(this.data.comments)
      })
    });

  },
  purchaseNow:function(e){
    wx.showLoading()
    console.log(e)
    let id = e.currentTarget.dataset.id
    let type ='medic'
    db.collection('CartsData').where({
      UserID:app.userDate.userInfo.userPhone,
      CartsID:id
    }).get().then(res=>{
      console.log(res.data[0])
      if(res.data[0]!=undefined){
        // wx.showToast({
        //   title: '商品已加入订单了哦~',
        //   icon:'none'
        // })
        wx.hideLoading()
        wx.navigateTo({
          url: '../order/order',
        })
      }else{
        db.collection('CartsData').add({
          data:{
            UserID:app.userDate.userInfo.userPhone,
            className:type,
            CartsID:id,
          }
      }).then(res => {
        console.log(res)
        if(res.errMsg== "collection.add:ok"){
            wx.hideLoading()
            wx.navigateTo({
              url: '../order/order',
            })
        }else{
          wx.showToast({
            title: '跳转失败',
            icon:'/images/icon/defeat.png'
          })
        }
      })
      .catch(console.error)
      }
    })
  },
  addtoorder:function(e){
    wx.showLoading()
    console.log(e)
    let id = e.currentTarget.dataset.id
    let type ='medic'
    db.collection('CartsData').where({
      UserID:app.userDate.userInfo.userPhone,
      CartsID:id
    }).get().then(res=>{
      console.log(res.data[0])
      if(res.data[0]!=undefined){
        wx.showToast({
          title: '商品已加入订单了哦~',
          icon:'none'
        })
        wx.hideLoading()
      }else{
        db.collection('CartsData').add({
          data:{
            UserID:app.userDate.userInfo.userPhone,
            className:type,
            CartsID:id,
            // GoodsName:name,
            // GoodsPrice:price,
            // GoodsImage:img,
            // GoodsNum:num,
            // checked:false,
            // date:util.formatDateTime(new Date())
          }
      }).then(res => {
        console.log(res)
        if(res.errMsg== "collection.add:ok"){
            wx.showToast({
              title: '成功添加至订单',
            })
            wx.hideLoading()
        }else{
          wx.showToast({
            title: '添加失败',
            icon:'/images/icon/defeat.png'
          })
        }
      })
      .catch(console.error)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  handleDetail() {
    let list = JSON.stringify(this.data.comments)
    wx.navigateTo({
      url: '/pages/guest/store/commentDetail/commentDetail?commentsID=' + this.data.detail.MedicID
    })
  }
})